package jiankao;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class AllResultSheetForPrint {

    private XSSFWorkbook wb;
    private XSSFSheet sheet;
    private CellStyle dataCellStyle;
    private int cols;
    private int rows=2;//行标
    private String[] flags;
    private String filePath;
    private static boolean isFirst=true;
    private FileOutputStream fileOutputStream;

    public AllResultSheetForPrint(String filePath,String sheetName,int cols){
        this.cols=cols;
        this.filePath=filePath;
        File file=new File(filePath);
        if(!AllResultSheetForPrint.isFirst&&file.exists()){
            try {
                this.wb=new XSSFWorkbook(new FileInputStream(file));
            } catch (IOException e) {
                e.printStackTrace();
                this.wb=new XSSFWorkbook();
            }
        }else{
            this.wb=new XSSFWorkbook();
            AllResultSheetForPrint.isFirst=false;
        }
        this.sheet=wb.createSheet(sheetName);
        setColWidth(new int[]{10,10,12,10,10,10,10,10});
    }

    public void setColWidth(int[] widths){
        for(int i=0;i<widths.length;i++){
            sheet.setColumnWidth(i, widths[i]*256);
        }
    }

    private CellStyle getDataCellStyle(){
        if(dataCellStyle==null){
            dataCellStyle=wb.createCellStyle();
            dataCellStyle.setBorderTop(BorderStyle.THIN);//上边框
            dataCellStyle.setBorderBottom(BorderStyle.THIN); //下边框
            dataCellStyle.setBorderLeft(BorderStyle.THIN);//左边框
            dataCellStyle.setBorderRight(BorderStyle.THIN);//右边框
            dataCellStyle.setVerticalAlignment(dataCellStyle.getVerticalAlignmentEnum().CENTER);
            dataCellStyle.setAlignment(HorizontalAlignment.CENTER);
            dataCellStyle.setWrapText(true);
            Font font = wb.createFont();
            font.setBold(true);
            font.setFontName("宋体");
            dataCellStyle.setFont(font);
        }
        return dataCellStyle;
    }

    private void setRowHeight(int height){
        sheet.getRow(rows-1).setHeight((short) (height*20));
    }

    private CellStyle getTitleCellStyle(){
        XSSFCellStyle cellStyle=wb.createCellStyle();
        cellStyle.setBorderTop(BorderStyle.THIN);//上边框
        cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
        cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
        cellStyle.setBorderRight(BorderStyle.THIN);//右边框
        cellStyle.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setWrapText(true);
        Font font = wb.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 20);
        font.setFontName("宋体");
        cellStyle.setFont(font);
        return cellStyle;
    }

    public void setTitle(String title){
        CellRangeAddress range=new CellRangeAddress(0,0,0,cols-1);
        sheet.addMergedRegion(range);
        XSSFRow row=sheet.createRow(0);
        row.setHeight((short) (39.75*20));
        for(int i=0;i<cols;i++){
            XSSFCell cell=row.createCell(i);
            cell.setCellStyle(getTitleCellStyle());
        }
        row.getCell(0).setCellValue(title);
    }

    public void setFlags(String[] flags){
        this.cols=flags.length;
        XSSFRow row=sheet.createRow(1);
        row.setHeight((short) (33*20));
        int index=0;
        for(String e:flags){
            XSSFCell cell=row.createCell(index);
            cell.setCellValue(e);
            cell.setCellStyle(getDataCellStyle());
            index++;
        }
        this.flags=flags;
    }

    public void setDatas(List<Map<String,Object>> datas){
        Iterator<Map<String,Object>> iterator=datas.iterator();
        while (iterator.hasNext()){
            Map<String,Object> data=iterator.next();
            XSSFRow row=sheet.createRow(rows++);
            setRowHeight(18);
            for(int index=0;index<flags.length;index++){
                XSSFCell cell=row.createCell(index);
                System.out.println(flags[index]);
                cell.setCellValue(data.get(flags[index]).toString());
                cell.setCellStyle(getDataCellStyle());
            }
        }
    }

    public boolean write(){
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            this.fileOutputStream=fileOutputStream;
            wb.write(fileOutputStream);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public void close(){
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
